<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- 引入样式文件 -->
        <link rel="stylesheet" href="{{ asset('css/vant.index.css') }}"/>
        <title>数治科技</title>
    </head>
    <body class="antialiased">
        <!-- <p>用户ID：{{ $user['userId'] }}</p>
        <p><button class="sendMsg">发送消息</button></p>
        <p><button class="getUserId">获取外部用户ID</button></p> -->
        <!-- <p class="content"></p> -->
    </body>

    <!-- 引入 Vue 和 Vant 的 JS 文件 -->
    <script src="{{ asset('js/vue.min.js') }}"></script>
    <script src="{{ asset('js/vant.min.js') }}"></script>
    <script src="{{ asset('js/jquery.min.js') }}"></script>
    <script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://res.wx.qq.com/wwopen/js/jsapi/jweixin-1.0.0.js"></script>
    <script>
        const toast = vant.Toast.loading({
            message: '加载中...',
            forbidClick: true,
            duration: 0
        });

        let callback = '<?php echo $callback;?>';
        let staffId = '<?php echo $contact['userid'];?>';
        let staffName = '<?php echo $contact['name'];?>';
        let qy = '<?php echo $qy;?>';
        wx.config(<?php echo $config;?>);

        wx.ready(function() {
            wx.agentConfig(<?php echo $agentConfig;?>)
            setTimeout(() => {

                wx.invoke('getContext', {}, function(res) {
                    if (res.err_msg == 'getContext:ok') {
                        if (res.entry == 'single_chat_tools' || res.entry == 'contact_profile') {
                            wx.invoke('getCurExternalContact', { }, function(res) {
                                if(res.err_msg == "getCurExternalContact:ok") {
                                    userId  = res.userId ; //返回当前外部联系人userId
                                    $.ajax({
                                        type: "GET",
                                        url: "/api/transfer",
                                        data: {
                                            callback: callback,
                                            url: 'sys/thirdApp/wechatEnterprise/login',
                                            method: 'get',
                                            payload: JSON.stringify({ userId: userId })
                                        },
                                        dataType: "json",
                                        success: function(data) {
                                            if (data.success === true) {
                                                const dot = (callback.indexOf('?') != -1) ? '&' : '?'
                                                callback = callback + dot + 'userid=' + data.result + '&staff_id=' + staffId + '&staff_name=' + staffName + '&qy=' + qy
                                                // alert(callback)
                                                window.location.href = callback
                                                toast.clear()
                                            } else {
                                                alert('API：' + data.message)
                                            }
                                        },
                                        error: function(err) {
                                            alert('系统错误')
                                        }
                                    })
                                } else {
                                    //错误处理
                                    alert('error：' + JSON.stringify(res))
                                }
                            });
                        } else {
                            wx.invoke('getCurExternalChat', { }, function(res) {
                                if(res.err_msg == "getCurExternalChat:ok") {
                                    chatId  = res.chatId ; //返回当前客户群的群聊ID
                                    // alert('/chat/' + chatId + '?callback=' + callback)
                                    // return false;
                                    window.location.href = '/customers/' + chatId + '?staff_id=' + staffId + '&staff_name=' + staffName +' &callback=' + callback;
                                    // alert(chatId + '：暂不支持从群聊入口查看患者详情')
                                } else {
                                    //错误处理
                                    alert('error：' + JSON.stringify(res))
                                }
                            });
                        }
                    } else {
                        //错误处理
                        alert('getContextError：' + JSON.stringify(res))
                    }
                })

            }, 1000);

        });

        wx.error(function(res) {
            alert(JSON.stringify(res))
        });
    </script>
</html>
